<%@ page language="java" pageEncoding="utf-8"%>
<%@ taglib prefix="my" uri="myTag/core"%>
<!DOCTYPE html>
<html>
	<head>
		<title>调查列表</title>
		<%@include file="/script/myJs/common.jspf"%>
		<script type="text/javascript" src="script/ckeditor/ckeditor.js"></script>
	</head>
	<body>
		<div class="easyui-layout" data-options="fit:true">
			<!-- 左侧调查分类菜单 -->
			<div data-options="region:'west',border:false" style="width: 240px;padding:5px">
				<div class="easyui-panel" data-options="fit:true">
					<ul id="surveyTypeTree"></ul>
					<div id="surveyTypeTreeMenu" class="easyui-menu" style="width:120px;">
						<my:auth url="survey/toAdd"><div onclick="toSurveyAdd()" data-options="iconCls:'icon-add'">添加</div></my:auth>
						<div class="menu-sep"></div>
						<div onclick="surveyTypeTreeFlush()" data-options="iconCls:'icon-reload'">刷新</div>
					</div>
				</div>
			</div>
			<div data-options="region:'center',border:false">
				<%-- 调查查询条件 --%>
				<div id="surveyToolbar" style="padding:0 30px;">
					<div class="conditions">
						<form id="surveyQueryForm">
							<input type="hidden" id="survey_one" name="one">
							<span class="con-span">名称：</span>
							<input name="two" class="easyui-textbox" style="width:166px;height:35px;line-height:35px;">
							<span class="con-span">问卷：</span>
							<input name="three" class="easyui-textbox" style="width:166px;height:35px;line-height:35px;">
							<a href="javascript:void(0);" class="easyui-linkbutton" iconCls="icon-search" data-options="selected:true" onclick="surveyQuery();">查询</a>
							<a href="javascript:void(0);" class="easyui-linkbutton" iconCls="icon-clear" onclick="surveyReset();">重置</a>
						</form>
					</div>
					<div class="opt-buttons">
						<my:auth url="survey/toAdd"><a href="javascript:void(0);" class="easyui-linkbutton" iconCls="icon-add" data-options="selected:true" onclick="toSurveyAdd();">添加</a></my:auth>
						<my:auth url="survey/toEdit"><a href="javascript:void(0);" class="easyui-linkbutton" iconCls="icon-edit" onclick="toSurveyEditForBtn();">修改</a></my:auth>
						<my:auth url="survey/doDel"><a href="javascript:void(0);" class="easyui-linkbutton" iconCls="icon-remove" onclick="doSurveyDelForBtn();">删除</a></my:auth>
						<my:auth url="survey/toSurveyTypeUpdate"><a href="javascript:void(0);" class="easyui-linkbutton" iconCls="icon-edit" onclick="toSurveyTypeUpdate();">设置分类</a></my:auth>
						<my:auth url="survey/doPublish"><a href="javascript:void(0);" class="easyui-linkbutton" iconCls="icon-edit" onclick="doPublish();">发布</a></my:auth>
						<my:auth url="survey/toSurveyUrl"><a href="javascript:void(0);" class="easyui-linkbutton" iconCls="icon-edit" onclick="toSurveyUrl();">分享调查链接</a></my:auth>
					</div>
				</div>
				<%-- 调查数据表格 --%>
				<table id="surveyGrid">
				</table>
			</div>
		</div>
	</body>
	<script type="text/javascript">
		//定义变量
		var surveyGrid = $("#surveyGrid"); //调查表格对象
		var surveyQueryForm = $("#surveyQueryForm"); //调查查询对象
		var surveyTypeTree = $("#surveyTypeTree"); //调查分类树对象
		var curSelSurveyTypeId = ""; //当前选中的调查分类ID
		var curSelSurveyTypeName = ""; //当前选中的调查分类名称
		var toolbar = [
		                //加粗     斜体，     下划线      穿过线      下标字        上标字
		                ["Bold","Italic","Underline","Strike","Subscript","Superscript"],
		                // 数字列表          实体列表            减小缩进    增大缩进
		                ["NumberedList","BulletedList","-","Outdent","Indent"],
		                //左对 齐             居中对齐          右对齐          两端对齐
		                ["JustifyLeft","JustifyCenter","JustifyRight","JustifyBlock"],
		                //超链接  取消超链接 锚点
		                ["Link","Unlink","Anchor"],
		                //图片    flash    表格       水平线            表情       特殊字符        分页符
		                ["Image","Flash","Table","HorizontalRule","Smiley","SpecialChar","PageBreak"],
		                // 样式       格式      字体    字体大小
		                ["Styles","Format","Font","FontSize"],
		                //文本颜色     背景颜色
		                ["TextColor","BGColor"]];
	
		//页面加载完毕，执行如下方法：
		$(function() {
			initSurveyGrid();
			initSurveyTypeTree();
		});
	
		//初始化调查表格
		function initSurveyGrid() {
			surveyGrid.datagrid( {
				url : "",
				onDblClickRow : <my:auth url="survey/toEdit">toSurveyEditForDblClick</my:auth>,
				toolbar : "#surveyToolbar",
				columns : [[ 
						{field : "ID", title : "", checkbox : true}, 
						{field : "NAME", title : "名称", width : 80, align : "center"},
						{field : "PAPER_NAME", title : "问卷", width : 80, align : "center"},
						{field : "START_TIME_STR", title : "开始时间", width : 80, align : "center"},
						{field : "END_TIME_STR", title : "结束时间", width : 80, align : "center"},
						{field : "SURVEY_TYPE_NAME", title : "调查分类", width : 80, align : "center"},
						{field : "STATE_NAME", title : "状态", width : 80, align : "center"}
						]]
			});
		}
		
		//初始化调查分类树
		function initSurveyTypeTree(){
			surveyTypeTree.tree({
				idFiled : "ID",
				textFiled : "NAME",
				parentField : "PARENT_ID",
				iconClsFiled : "ICON",
				checkedFiled : "CHECKED",
				lines : true,
			    url : "survey/surveyTypeTreeList",
				onContextMenu : function(e, node){
					e.preventDefault();
					$(this).tree("select", node.target);
					$("#surveyTypeTreeMenu").menu("show", {
						left : e.pageX,
						top : e.pageY
					})
				},
				onSelect : function(node){
					curSelSurveyTypeId = node.ID;
					curSelSurveyTypeName = node.NAME;
					
					$("#survey_one").val(curSelSurveyTypeId);
					surveyGrid.datagrid("uncheckAll");
					surveyGrid.datagrid("reload", $.fn.my.serializeObj(surveyQueryForm));
				},
				onLoadSuccess : function(node, data){
					if(!curSelSurveyTypeId || !surveyGrid.datagrid("options").url){//如果是第一次
						curSelSurveyTypeId = 1;
						surveyGrid.datagrid("options").url = "survey/list";
					}
					
					var node = surveyTypeTree.tree("find", curSelSurveyTypeId);
					if(!node){
						curSelSurveyTypeId = 1;
						node = surveyTypeTree.tree("find", curSelSurveyTypeId);
					}
					surveyTypeTree.tree("select", node.target);
				}
			});
		}
	
		//调查查询
		function surveyQuery() {
			surveyGrid.datagrid("uncheckAll");
			surveyGrid.datagrid("load", $.fn.my.serializeObj(surveyQueryForm));
		}
	
		//调查重置
		function surveyReset() {
			surveyQueryForm.form("reset");
			surveyQuery();
		}
		
		//到达添加调查页面
		function toSurveyAdd() {
			if(!curSelSurveyTypeId){
				parent.$.messager.alert("提示消息", "请选择调查分类！", "info");
				return;
			}
			
			var surveyAddDialog;
			surveyAddDialog = $("<div/>").dialog({
				title : "添加调查",
				href : "survey/toAdd",
				buttons : [{
					text : "添加",
					iconCls : "icon-add",
					selected : true,
					handler : function(){
						doSurveyAdd(surveyAddDialog);
					}
				}],
				onLoad : function() {
					$("#survey_surveyTypeId").val(curSelSurveyTypeId);
					$("#survey_surveyTypeName").val(curSelSurveyTypeName);
					
					$("#survey_name").textbox({
						required : true,
						validType : ["length[1, 16]"]
					});
					$("#survey_paperId").textbox({
						required : true,
						editable : false,
						icons : [{
							iconCls : "icon-add",
							handler : function(e){
								toPaperAddList();
							}
						}]
					});
					$("#survey_startTime").datetimebox({
						required : true,
						editable : false
					});
					$("#survey_endTime").datetimebox({
						required : true,
						editable : false
					});
					CKEDITOR.replace("survey_description", {
						height: "80px",
			        	toolbar : toolbar
					});
				}
			});
		}

		//完成添加调查
		function doSurveyAdd(surveyAddDialog) {
			var surveyEditFrom = $("#surveyEditFrom");
			if(!surveyEditFrom.form("validate")){
				return;
			}
			
			$.messager.confirm("提示消息", "确定要添加？", function(ok){
				if (!ok){
					return;
				}
				
				surveyEditFrom.form("submit", {
					url : "survey/doAdd",
					success : function(data) {
						surveyGrid.datagrid("reload");
						$.messager.progress("close");

						var obj = $.parseJSON(data);
						if (!obj.success) {
							parent.$.messager.alert("提示消息", obj.message, "info");
							return;
						}

						surveyAddDialog.dialog("close");
					}
				});
			});
		}

		//到达修改调查页面
		function toSurveyEdit(id) {
			var surveyEditDialog;
			surveyEditDialog = $("<div/>").dialog({
				title : "修改调查",
				href : "survey/toEdit",
				queryParams : {"id" : id},
				buttons : [ {
					text : "修改",
					iconCls : "icon-edit",
					selected : true,
					handler : function() {
						doSurveyEdit(surveyEditDialog);
					}
				} ],
				onLoad : function() {
					$("#survey_name").textbox({
						required : true,
						validType : ["length[1, 16]"]
					});
					
					$("#survey_paperId").textbox({
						required : true,
						editable : false,
						icons : [{
							iconCls : "icon-add",
							handler : function(e){
								toPaperAddList();
							}
						}]
					});
					$("#survey_paperId").textbox("setText", $("#survey_paperName").val());
					$("#survey_startTime").datetimebox({
						required : true,
						editable : false
					});
					$("#survey_endTime").datetimebox({
						required : true,
						editable : false
					});
					CKEDITOR.replace("survey_description", {
						height: "80px",
			        	toolbar : toolbar
					});
				}
			});
		}

		//到达修改调查页面
		function toSurveyEditForBtn() {
			var surveyGridRows = surveyGrid.datagrid("getChecked");
			if (surveyGridRows.length != 1) {
				parent.$.messager.alert("提示消息", "请选择一行数据！", "info");
				return;
			}
			if(surveyGridRows[0].STATE == "1"){
				parent.$.messager.alert("提示消息", "【"+surveyGridRows[0].NAME + "】已" + surveyGridRows[0].STATE_NAME + "！", "info");
				return;
			}

			toSurveyEdit(surveyGridRows[0].ID);
		}

		//到达修改调查页面
		function toSurveyEditForDblClick(rowIndex, rowData) {
			surveyGrid.datagrid("uncheckAll");
			surveyGrid.datagrid("checkRow", rowIndex);
			toSurveyEditForBtn();
		}

		//完成修改调查
		function doSurveyEdit(surveyEditDialog) {
			var surveyEditFrom = $("#surveyEditFrom");
			if(!surveyEditFrom.form("validate")){
				return;
			}
			$.messager.confirm("确认消息", "确定要修改？", function(ok) {
				if (!ok) {
					return;
				}
				
				$.messager.progress();
				surveyEditFrom.form("submit", {
					url : "survey/doEdit",
					success : function(data) {
						surveyGrid.datagrid("reload");
						$.messager.progress("close");
	
						var obj = $.parseJSON(data);
						if (!obj.success) {
							parent.$.messager.alert("提示消息", obj.message, "info");
							return;
						}
	
						surveyEditDialog.dialog("close");
					}
				});
			})
		}

		//完成删除调查
		function doSurveyDel(params) {
			$.messager.confirm("确认消息", "确定要删除？", function(ok) {
				if (!ok) {
					return;
				}

				$.messager.progress();
				$.ajax({
					url : "survey/doDel",
					data : params,
					success : function(obj) {
						surveyGrid.datagrid("reload");
						$.messager.progress("close");
						
						if (!obj.success) {
							parent.$.messager.alert("提示消息", obj.message, "info");
						}
					}
				});
			});
		}

		//完成删除调查
		function doSurveyDelForBtn() {
			//校验数据有效性
			var surveyGridRows = surveyGrid.datagrid("getChecked");
			if (surveyGridRows.length == 0) {
				parent.$.messager.alert("提示消息", "请选择一行或多行数据！", "info");
				return;
			}

			//删除
			doSurveyDel($.fn.my.serializeField(surveyGridRows));
		}
		
		//到达添加问卷页面
		function toPaperAddList(){
			var paperAddListDialog;
			paperAddListDialog = $("<div/>").dialog({
				title : "问卷列表",
				href : "survey/toPaperAddList",
				maximized : true,
				buttons : [{
					text : "添加",
					iconCls : "icon-add", 
					selected : true, 
					handler : function (){
						doPaperAdd(paperAddListDialog);
					}
				}],
				onLoad : function() {
					var paperAddListGrid = $("#paperAddListGrid");
					paperAddListGrid.datagrid( {
						toolbar : "#paperAddListToolbar",
						columns : [[ 
								{field : "ID", title : "", checkbox : true},
								{field : "NAME", title : "名称", width : 50, align : "center"},
								{field : "STATE_NAME", title : "状态", width : 80, align : "center"}
								]]
					});
					
					var paperAddListTypeTree = $("#paperAddListTypeTree");
					var paperAddListQueryForm = $("#paperAddListQueryForm");
					var curSelPaperTypeId = "";
					var curSelPaperTypeName = "";
					paperAddListTypeTree.tree({
						idFiled : "ID",
						textFiled : "NAME",
						parentField : "PARENT_ID",
						iconClsFiled : "ICON",
						checkedFiled : "CHECKED",
						lines : true,
					    url : "survey/paperAddListTypeTreeList",
						onSelect : function(node){
							curSelPaperTypeId = node.ID;
							curSelPaperTypeName = node.NAME;
							
							$("#paperAddList_one").val(curSelPaperTypeId);
							paperAddListGrid.datagrid("uncheckAll");
							paperAddListGrid.datagrid("reload", $.fn.my.serializeObj(paperAddListQueryForm));
						},
						onLoadSuccess : function(node, data){
							if(!curSelPaperTypeId || !paperAddListGrid.datagrid("options").url){//如果是第一次
								curSelPaperTypeId = 1;
								paperAddListGrid.datagrid("options").url = "survey/paperAddList";
							}
							
							var node = paperAddListTypeTree.tree("find", curSelPaperTypeId);
							if(!node){
								curSelPaperTypeId = 1;
								node = paperAddListTypeTree.tree("find", curSelPaperTypeId);
							}
							paperAddListTypeTree.tree("select", node.target);
						}
					});
				}
			});
		}
		
		//添加问卷查询
		function paperAddListQuery(){
			var paperAddListQueryForm = $("#paperAddListQueryForm");
			var paperAddListGrid = $("#paperAddListGrid");
			paperAddListGrid.datagrid("uncheckAll");
			paperAddListGrid.datagrid("load", $.fn.my.serializeObj(paperAddListQueryForm));
		}
	
		//添加问卷重置
		function paperAddListReset() {
			var paperAddListQueryForm = $("#paperAddListQueryForm");
			paperAddListQueryForm.form("reset");
			paperAddListQuery();
		}
		
		//完成添加问卷
		function doPaperAdd(paperAddListDialog){
			var paperGridRows = $("#paperAddListGrid").datagrid("getChecked");
			if (paperGridRows.length != 1) {
				parent.$.messager.alert("提示消息", "请选择一行数据！", "info");
				return;
			}
			
			$("#survey_paperId").textbox("setValue", paperGridRows[0].ID);
			$("#survey_paperId").textbox("setText", paperGridRows[0].NAME);
			paperAddListDialog.dialog("close");
		}
		
		//完成发布调查
		function doPublish() {
			//校验数据有效性
			var surveyGridRows = surveyGrid.datagrid("getChecked");
			if (surveyGridRows.length == 0) {
				parent.$.messager.alert("提示消息", "请选择一行或多行数据！", "info");
				return;
			}
			
			//发布
			var params = $.fn.my.serializeField(surveyGridRows);
			$.messager.confirm("确认消息", "确定要发布？", function(ok) {
				if (!ok) {
					return;
				}

				$.messager.progress();
				$.ajax({
					url : "survey/doPublish",
					data : params,
					success : function(obj) {
						surveyGrid.datagrid("reload");
						$.messager.progress("close");
						
						if (!obj.success) {
							parent.$.messager.alert("提示消息", obj.message, "info");
						}
					}
				});
			});
		}
		
		//到达分享调查链接页面
		function toSurveyUrl() {
			var surveyGridRows = surveyGrid.datagrid("getChecked");
			if (surveyGridRows.length != 1) {
				parent.$.messager.alert("提示消息", "请选择一行数据！", "info");
				return;
			}
			if (surveyGridRows[0].STATE == "2") {
				parent.$.messager.alert("提示消息", "【"+surveyGridRows[0].NAME + "】" + surveyGridRows[0].STATE_NAME + "！", "info");
				return;
			}

			var surveyUrlDialog;
			surveyUrlDialog = $("<div/>").dialog({
				title : "分享调查链接",
				href : "survey/toSurveyUrl",
				queryParams : {},
				buttons : [ {
					text : "关闭",
					iconCls : "icon-ok",
					selected : true,
					handler : function() {
						surveyUrlDialog.dialog("close");
					}
				} ],
				onLoad : function() {
					var url = window.location.origin + "${pageContext.request.contextPath}" + "/home/survey/pubToPaper?surveyId=" + surveyGridRows[0].ID;
					$("#survey_url").html(url);
				}
			});
		}
		
		//到达设置调查分类页面
		function toSurveyTypeUpdate(){
			var surveyGridRows = surveyGrid.datagrid("getChecked");
			if(surveyGridRows.length == 0){
				parent.$.messager.alert("提示消息", "请选择一行或多行数据！", "info");
				return;
			}
			
			var surveyTypeUpdateDialog;
			surveyTypeUpdateDialog = $("<div/>").dialog({
				title : "选择调查分类",
				width : 300,
				height : 400,
				href : "survey/toSurveyTypeUpdate",
				buttons : [{
					text : "确定", 
					iconCls : "icon-ok", 
					selected : true,
					handler : function (){
						doSurveyTypeUpdate(surveyTypeUpdateDialog);
					}
				}],
				onLoad : function(){
					var surveyTypeUpdateSurveyTypeTree = $("#surveyTypeUpdateSurveyTypeTree"); 
					surveyTypeUpdateSurveyTypeTree.tree({
						idFiled : "ID",
						textFiled : "NAME",
						parentField : "PARENT_ID",
						iconClsFiled : "ICON",
						checkedFiled : "CHECKED",
						lines : true,
					    url : "survey/surveyTypeUpdateSurveyTypeTreeList",
					});
				}
			});
		}
		
		//完成设置调查分类
		function doSurveyTypeUpdate(surveyTypeUpdateDialog){
			var surveyTypeUpdateSurveyTypeNode = $("#surveyTypeUpdateSurveyTypeTree").tree("getSelected");
			if(!surveyTypeUpdateSurveyTypeNode){
				parent.$.messager.alert("提示消息", "请选择一行数据！", "info");
				return;
			}
			
			$.messager.confirm("确认消息", "确定要设置？", function(ok){
				if (!ok){
					return;
				}
				
				var surveyGridRows = surveyGrid.datagrid("getChecked");
				var params = $.fn.my.serializeField(surveyGridRows)
				params += ("&surveyTypeId=" + surveyTypeUpdateSurveyTypeNode.ID);

				$.messager.progress();
				$.ajax({
					url : "survey/doSurveyTypeUpdate",
					data : params,
					success : function(obj){
						surveyTypeUpdateDialog.dialog("close");
						surveyTypeTree.tree("reload");
						$.messager.progress("close");
						
						if(!obj.success){
							parent.$.messager.alert("提示消息", obj.message, "info");
							return;
						}
					}
				});
			});
		}
		
		//刷新调查分类
		function surveyTypeTreeFlush(){
			surveyTypeTree.tree("reload");
		}
	</script>
</html>